﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using LevenBlog.Core.Entities;

namespace LevenBlog.Core.Data.Sqlite
{
    public sealed class SqliteTag : ITag
    {
        public int Count(IDbProvider provider)
        {
            string sql = "select count([id]) from [lb_tag]";
            return Convert.ToInt32(SqliteHelper.Instance.ExecuteScalar(provider, sql));
        }

        public void Insert(IDbProvider provider, lb_tag tag)
        {
            IDataParameter[] paras = null;
            string sql = SqliteHelper.Instance.CreateInsertSql(tag, out paras);
            SqliteHelper.Instance.ExecuteNonQuery(provider, sql, paras);
        }

        public void Update(IDbProvider provider, lb_tag tag)
        {
            IDataParameter[] paras = null;
            string sql = SqliteHelper.Instance.CreateUpdateSql(tag, out paras);
            SqliteHelper.Instance.ExecuteNonQuery(provider, sql, paras);
        }

        public void Delete(IDbProvider provider, int id)
        {
            string sql = string.Format("delete from [lb_tag] where [id]={0}", id);
            SqliteHelper.Instance.ExecuteNonQuery(provider, sql);
        }

        public void Delete(IDbProvider provider, string tagName)
        {
            string sql = string.Format("delete from [lb_tag] where [name]='{0}'", tagName);
            SqliteHelper.Instance.ExecuteNonQuery(provider, sql);
        }

        public lb_tag Get(IDbProvider provider, int id)
        {
            string sql = string.Format("select * from [lb_tag] where [id]={0}", id);
            using (IDataReader dr = SqliteHelper.Instance.ExecuteDataReader(provider, sql))
            {
                return SqliteHelper.Instance.ConvertToEntity<lb_tag>(dr);
            }
        }

        public lb_tag Get(IDbProvider provider, string tagName)
        {
            string sql = string.Format("select * from [lb_tag] where [name]='{0}'", tagName);
            using (IDataReader dr = SqliteHelper.Instance.ExecuteDataReader(provider, sql))
            {
                return SqliteHelper.Instance.ConvertToEntity<lb_tag>(dr);
            }
        }

        public IList<lb_tag> Get(IDbProvider provider, int pageSize, int currentPage)
        {
            if (currentPage < 1)
                currentPage = 1;
            //string sql = PageHelper.BuildPageSql(pageSize, currentPage, "*", "[lb_tag]", string.Empty, "[id]", "order by [id] desc");
            string sql = string.Format("select * from [lb_tag] order by [id] desc limit {0},{1}", (currentPage - 1) * pageSize, pageSize);
            using (IDataReader dr = SqliteHelper.Instance.ExecuteDataReader(provider, sql))
            {
                return SqliteHelper.Instance.ConvertToList<lb_tag>(dr);
            }
        }

        public IList<lb_tag> GetOrderByHot(IDbProvider provider, int pageSize, int currentPage)
        {
            if (currentPage < 1)
                currentPage = 1;
            //string sql = PageHelper.BuildPageSql(pageSize, currentPage, "*", "[lb_tag]", string.Empty, "[id]", "order by [num] desc");
            string sql = string.Format("select * from [lb_tag] order by [num] desc limit {0},{1}", (currentPage - 1) * pageSize, pageSize);
            using (IDataReader dr = SqliteHelper.Instance.ExecuteDataReader(provider, sql))
            {
                return SqliteHelper.Instance.ConvertToList<lb_tag>(dr);
            }
        }
    }
}
